<?php

class Controller extends Base
{

    protected $_token;
    protected $_view = null; //视图

    public function __construct(){
    }

    protected function _redirect($arr)
    {
        if(is_array($arr)){
            array_key_exists('c', $arr) || $arr['c'] = Path::getController();
            array_key_exists('a', $arr) || $arr['a'] = Path::getAction();;
            $str = 'http://' . Path::getBasePath() . '?';
            foreach ($arr as $key => $val) {
                if (!is_int($key)) {
                    $str .= ($key . '=' . $val . '&');
                }
            }
            $str = substr($str, 0, strlen($str) - 1);
            Response::redirect($str);
        } 
        if( is_string($arr) ){
            if($this->startsWith($arr,'http')==1){
                Response::redirect($arr);
            } else {
                $str = 'http://' . Path::getBasePath() . '?' . $arr;
                Response::redirect($str);
            }
        }
    }

    protected function _forward(Array $arr)
    {
        $controller = Path::getController();
        $action = Path::getAction();
        if (array_key_exists('controller', $arr)) {
            $controller = $arr['controller'];
        }
        if (array_key_exists('action', $arr)) {
            $action = $arr['action'];
        }
        $controller .= 'Controller';
        if ($controller === get_class()) {
            if (method_exists($this, $action)) {
                $this->$action();
            } else {
                throw new NoClassException(NoClassException::NONE_EXISTS_METHOD);
            }
        } else {
            if (class_exists($controller)) {
                $class = new $controller();
                if (method_exists($class, $action)) {
                    $class->$action();
                } else {
                    throw new NoClassException(NoClassException::NONE_EXISTS_METHOD);
                }
            } else {
                throw new NoClassException(NoClassException::NONE_EXISTS_CLASS);
            }
        }
    }

    protected function _assign($name,$value = '')
    {
        if(null === $this->_view) {
            $this->_view = new View();
        }
        $this->_view->assign($name,$value);
    }

    protected function _display($str)
    {
        if (is_string($str)) {
            $str = str_replace(array(
                '.', '#'
            ), array(
                '/', '.'
            ), $str);
            if(null === $this->_view) {
                $this->_view = new View();
            }
            $this->_view->display(MODULES_PATH . View::VIEW_BASE_PATH . $str . '.'.C('view=>suffix'));
        }
    }

    protected function _show($str){
        if (is_string($str)) {
            echo $str;
        }
        if (is_array($str)) {
            var_dump($str);
        }
    }

    private function _setToken()
    {
        if (empty($_COOKIE['_csrfToken'])) {
            $token = substr(md5(time()), 0, mt_rand(10, 15));
            $this->_token = $token;
            setcookie('_csrfToken', $token, time() + 3600 * 24 * 7);
        } else {
            $this->_token = $_COOKIE['_csrfToken'];
        }
    }

    protected function _getToken()
    {
        return $this->_token;
    }
    
    protected function startsWith($Haystack, $Needle){
        // Recommended version, using strpos
        return strpos($Haystack, $Needle) === 0;
    }

}